//
//  ConfigSubPhotoView.m
//  JinganMindCloud
//
//  Created by JinganMac on 2020/8/4.
//  Copyright © 2020 JingAn. All rights reserved.
//

#import "ConfigSubPhotoView.h"
#import "MCCPhotoCell.h"

@interface ConfigSubPhotoView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *leftTitleView;
@property (nonatomic, strong) UICollectionView *photoCollectView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSArray *arrayOfPhotos;
@end

@implementation ConfigSubPhotoView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.leftTitleView];
        [self addSubview:self.photoCollectView];
    }
    return self;
}

-(UILabel *)leftTitleView{
    if (!_leftTitleView) {
        _leftTitleView = [UILabel new];
        _leftTitleView.textColor = [UIColor colorWithHexString:@"#AEAEAE"];
        _leftTitleView.font = [UIFont systemFontOfSize:13.];
        _leftTitleView.numberOfLines = 0;
    }
    return _leftTitleView;
}


-(void)layoutSubviews{
    [_leftTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(5);
        make.width.mas_equalTo(self.leftViewWidth);
    }];
    
    [_photoCollectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftTitleView.mas_right);
        make.size.mas_equalTo(CGSizeMake([[UIScreen mainScreen] bounds].size.width-self.leftViewWidth-40, 70));
        make.top.bottom.right.mas_equalTo(self);
    }];
}

-(void)setViewConfiSets:(CellSubViewsConfigSetting *)viewConfiSets{
    self.leftTitleView.text = viewConfiSets.cTitleValue;
}


-(void)setPhotoArray:(NSArray *)array{
    self.arrayOfPhotos = array;
    
    [self.photoCollectView reloadData];
}

-(UICollectionView *)photoCollectView{
    if (!_photoCollectView) {
        _photoCollectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _photoCollectView.delegate = self;
        _photoCollectView.dataSource = self;
        _photoCollectView.backgroundColor = [UIColor clearColor];
        _photoCollectView.showsHorizontalScrollIndicator = NO;
        [_photoCollectView registerNib:[UINib nibWithNibName:@"MCCPhotoCell" bundle:[MCCToolClass MindCloudConfigCellBundle]] forCellWithReuseIdentifier:@"MCCPhotoCell"];
    }
    return _photoCollectView;
}

-(UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 5);
        _layout.minimumLineSpacing = 8;
        _layout.minimumInteritemSpacing = 10;
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.itemSize = CGSizeMake(60,60);
    }
    return _layout;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrayOfPhotos.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MCCPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MCCPhotoCell" forIndexPath:indexPath];
    cell.photoImage.layer.borderWidth  = 1.0f;
    cell.photoImage.layer.cornerRadius = 5.0f;
    cell.photoImage.layer.borderColor = [UIColor colorWithHexString:@"#DEDEDE"].CGColor;
    cell.photoImage.layer.masksToBounds = YES;
    NSDictionary *dict = self.arrayOfPhotos[indexPath.row];
    CCEnclosureFile *info = [CCEnclosureFile mj_objectWithKeyValues:dict];
    [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:info.webUrl] placeholderImage:[UIImage MCCImageNamed:@"ccimagedefault"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [MGJRouter openURL:@"MindCloud_ShowPhoto" withUserInfo:@{@"Photos":self.arrayOfPhotos,@"ShowIndex":@(indexPath.row)} completion:nil];
}



@end
